// pages/confirmOrder/confirmOrder.js
import request from '../../utils/request'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        takeTime: '',
        isLoading: false,
        placeInfo: {},
        placeList: [],
        goodsInfoList: [],
        userInfo: wx.getStorageSync('youxuan-user-userInfo'),
        goodsIds: "",
        // 状态栏高度
        statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',
        statusBarPaddingTop: wx.getStorageSync('statusBarHeight') + 50 + 'px',
        // 导航栏高度
        navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',
        // 胶囊按钮高度
        menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',
        // 导航栏和状态栏高度
        navigationBarAndStatusBarHeight:
            wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') +
            'px'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: async function (options) {
        console.log(options)
        this.setData({
            goodsIds: options.gids
        })
        let placeListResult = await request("/place/list")
        this.setData({
            placeList: placeListResult.data,
            placeInfo: placeListResult.data[0]
        })
        this.setData({
            placeImage: 'http://rc48dvuay.bkt.clouddn.com/' + placeListResult.data[0].imgUrl
        })
        console.log(placeListResult.data)

        var gids = options.gids.split(",")
        console.log(gids)
        let goodsInfoList = await this.getGoodsList(gids)
        this.setData({
            goodsInfoList: goodsInfoList
        })
        console.log(goodsInfoList)

        var time = Date.now()
        time = time + (60 * 60 * 24 * 2 * 1000)
        let d = this.getDate(time)
        console.log(d)
        this.setData({
            takeTime: d
        })
    },
    async getGoodsList(gids) {
        var goodsInfoList = []
        for (let i = 0; i < gids.length; i++) {
            var goodsInfo = await request("/cartdetail/detail?gid=" + gids[i] + "&uid=" + this.data.userInfo.id, {}, "POST")
            goodsInfoList.push(goodsInfo.data)
        }
        return goodsInfoList
    },
    async onSubmit() {
        this.setData({
            isLoading: true
        })

        let result = await request("/order/add", { goodsIds: this.data.goodsIds, placeId: this.data.placeInfo.id, userId: this.data.userInfo.id }, "POST")
        console.log(result)
        if (result.code == 200) {
            this.setData({
                isLoading: false
            })
            wx.showToast({
                title: '下单成功',
            })
            wx.redirectTo({
                url: '/pages/paySuccess/paySuccess?orderCode=' + result.data.orderCode,
            })
            return
        }

    },
    backBtn() {
        wx.navigateBack({
            delta: 1,
        })
    },
    getDate(date) {
        //date是传过来的时间戳，注意需为13位，10位需*1000
        //也可以不传,获取的就是当前时间
        var time = new Date(date);
        var month = time.getMonth() + 1; //月
        var day = time.getDate() //日
        var mydate = month + "月" + day + "日";
        return mydate
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})